<?php

class Core_MysqlException extends Exception{
}

/*
	For unique connection informations, Core_Mysql is singleton.
*/
class Core_Mysql{

	private $conn;

	// Init connection
	public function __construct($name, $host, $port, $user, $pwd){/*{{{*/
		if (empty($name) || empty($host) || empty($port) || empty($user) || empty($pwd)){
			throw new Core_MysqlException("Empty database connection info");
		}

		if(!($this->conn = mysql_connect("${host}:${port}",$user,$pwd))){
			throw new Core_MysqlException("Can't connect to ${host}:${port}, with $user@$pwd");
		}

		mysql_set_charset('utf8', $this->conn);
 
		if(!mysql_select_db($name,$this->conn))
		{
			mysql_close($this->conn);
			throw new Core_MysqlException("Can't select database $name");
		}
	}/*}}}*/

	public function __destruct(){/*{{{*/
		$this->close();
	}/*}}}*/

	// Close connection and set conn=null
	public function close(){/*{{{*/
		if ($this->_valid_conn()){
			mysql_close($this->conn);
		}
		$this->conn = null;
	}/*}}}*/

	// Query and get one line result as hash
	public function& query($sql){/*{{{*/
		$result = $this->_execute($sql);

		$ret = mysql_fetch_assoc($result); 
		return $ret;
	}/*}}}*/

	// Query and get multi line results as array(hash)
	public function& querys($sql){/*{{{*/
		$result = $this->_execute($sql);

		$rows = array();	
		while($row = mysql_fetch_assoc($result)){
			array_push($rows,$row);
		}

		return $rows;
	}/*}}}*/

	public function querys_total($sql){/*{{{*/
		return array(
			'info' => $this->querys($sql),
			'total' => $this->total(),
			);
	}/*}}}*/

	// Query and don't get result
	public function update($sql){/*{{{*/
		$result = $this->_execute($sql);

		return mysql_affected_rows($this->conn);
	}/*}}}*/

	// Get last insert id.
	public function last_id(){/*{{{*/
		$sql = 'select LAST_INSERT_ID() as iid';
		$result = $this->query($sql);

		return $result['iid'];
	}/*}}}*/

	// Get total count, Last sql should have "SQL_CALC_FOUND_ROWS", like "SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name"
	public function total(){/*{{{*/
		$sql = 'select FOUND_ROWS() as cnt';
		$result = $this->query($sql);

		return $result['cnt'];
	}/*}}}*/

	// Get this database last error msg
	public function error(){/*{{{*/
		if (!$this->_valid_conn()){
			throw new Core_MysqlException("Not valid connection");
		}

		return mysql_errno($this->conn).': '.mysql_error($this->conn);
	}/*}}}*/

################# private ##########################

	// Base query, return mysql_result handler
	private function _execute($sql){/*{{{*/
		#Core_Log::get_instance()->notice($sql);
		if (empty($sql)){
			throw new Core_MysqlException("Empty query");
		}

		if (!$this->_valid_conn()){
			throw new Core_MysqlException("Not valid connection");
		}


		if($result=mysql_query($sql,$this->conn)){
		} else {
			throw new Core_MysqlException("Query($sql) fail, ".$this->error());
		}

		return $result;
	}/*}}}*/

	// Check connection valid
	private function _valid_conn(){/*{{{*/
		return is_resource($this->conn);
	}/*}}}*/


}
